package com.weiyin.examination.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.weiyin.examination.R;
import com.weiyin.examination.activity.LoadingActivity;
import com.weiyin.examination.activity.PayActivity;
import com.weiyin.examination.adapter.TopicsAdapter;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.view.DividerItemDecoration;

import java.util.ArrayList;

/**
 * <pre>
 *     e-mail : niejunfeng@win-electr.com
 *     time   : 2018/04/03
 *     desc   :题库
 *     version: 1.0.0
 *     Copyright: Copyright（c）2017
 *     Company:上海蔚音电子科技有限公司
 * </pre>
 *
 * @author niejunfeng
 */
public class QuestionBankFragment extends BaseFragment implements TopicsAdapter.OnItemClickListener {
    private View view;
    private LayoutHelper layoutHelper;
    private RecyclerView topicsRecyclerView, examRecyclerView;

    private ArrayList<String> topicsData, examData;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.question_fragment_layout, null);
            layoutHelper = new LayoutHelper(getActivity());
            layoutHelper.scaleView(view);
            initData();
            initView();
        }
        return view;
    }

    public void initData() {
        if (topicsData == null) {
            topicsData = new ArrayList<>();
        }
        if (examData == null) {
            examData = new ArrayList<>();
        }
        for (int i = 0; i < 5; i++) {
            topicsData.add("专题" + i);
        }
        for (int i = 0; i < 5; i++) {
            examData.add("考试" + i);
        }
    }

    public void initView() {
        topicsRecyclerView = (RecyclerView) view.findViewById(R.id.topicsRecyclerView);
        examRecyclerView = (RecyclerView) view.findViewById(R.id.examRecyclerView);

        ImageView back = (ImageView) view.findViewById(R.id.back);
        back.setVisibility(View.GONE);

        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText("题库");

        TopicsAdapter topicsAdapter = new TopicsAdapter(getActivity(), R.layout.topic_item_layout, topicsData, "1");
        TopicsAdapter examAdapter = new TopicsAdapter(getActivity(), R.layout.topic_item_layout, examData, "2");

        topicsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        examRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        topicsRecyclerView.addItemDecoration(new DividerItemDecoration(
                getActivity(), DividerItemDecoration.VERTICAL_LIST, 3, getResources().getColor(R.color.CL)));
        examRecyclerView.addItemDecoration(new DividerItemDecoration(
                getActivity(), DividerItemDecoration.VERTICAL_LIST, 3, getResources().getColor(R.color.CL)));

        topicsRecyclerView.setAdapter(topicsAdapter);
        examRecyclerView.setAdapter(examAdapter);

        topicsAdapter.setOnItemClickListener(this);
        examAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseViewHolder baseViewHolder, int position, String type) {
        startActivity(new Intent(getContext(), PayActivity.class));
    }
}
